Allow JSON columns to be used as the dateColumn in MySQL#83
Allow JSON columns to be used as the dateColumn in MySQL#83SentisMR wants to merge 3 commits intoFlowframe:masterfrom
Conversation
sarpavci
left a comment
There was a problem hiding this comment.
In order to use MySqlGrammar you need to provide the connection information. You also need to add the Trend class.
Lines 171 to 181 in 5ace11d
protected function getSqlDate(): string
{
$connection = $this->builder->getConnection();
$adapter = match ($connection->getDriverName()) {
'mysql', 'mariadb' => new MySqlAdapter($connection),
'sqlite' => new SqliteAdapter(),
'pgsql' => new PgsqlAdapter(),
default => throw new Error('Unsupported database driver.'),
};
return $adapter->format($this->dateColumn, $this->interval);
}
src/Adapters/MySqlAdapter.php
Outdated
| namespace Flowframe\Trend\Adapters; | ||
|
|
||
| use Error; | ||
| use Illuminate\Database\Query\Grammars\MySqlGrammar; |
There was a problem hiding this comment.
| use Illuminate\Database\Query\Grammars\MySqlGrammar; | |
| use Illuminate\Database\ConnectionInterface; | |
| use Illuminate\Database\Query\Grammars\MySqlGrammar; |
src/Adapters/MySqlAdapter.php
Outdated
|
|
||
| $wrappedColumn = (new MySqlGrammar)->wrap($column); | ||
| return "date_format({$wrappedColumn}, '{$format}')"; |
There was a problem hiding this comment.
| $wrappedColumn = (new MySqlGrammar)->wrap($column); | |
| return "date_format({$wrappedColumn}, '{$format}')"; | |
| $wrappedColumn = (new MySqlGrammar($this->connection))->wrap($column); | |
| return "date_format({$wrappedColumn}, '{$format}')"; |
sarpavci
left a comment
There was a problem hiding this comment.
Or basically you can do:
Lines 171 to 181 in 5ace11d
protected function getSqlDate(): string
{
$adapter = match ($this->builder->getConnection()->getDriverName()) {
'mysql', 'mariadb' => new MySqlAdapter(),
'sqlite' => new SqliteAdapter(),
'pgsql' => new PgsqlAdapter(),
default => throw new Error('Unsupported database driver.'),
};
return $adapter->format(
column: $this->builder->getGrammar()->wrap($this->dateColumn), // <--- HERE
interval: $this->interval
);
}|
I think that the only place the connection gets used (in the mysql grammar anyways) is escaping raw sql and doing upserts. The code as written works in our environment. But your suggestion of grabbing the connection in the trend instead and pulling the builders grammar is cleaner. Thanks @sarpavci |
| default => throw new Error('Invalid interval.'), | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
I think you can checkout this file :)
|
@Larsklopstra Please take a look at this PR. |
What
This PR allows passes the dateColumn through the Illuminate MySQL grammar wrapper so that you can use JSON columns. Fixes #71
Why
We have dates in JSON columns that we would like to use to trend. The "where_between" call already allows this but the "date_format" does not.
How
Simply call the MySqlGrammar->wrap on the column name instead of injecting the string directly into the date format call.